<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Generics in Java</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Generics_in_Java"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Generics_in_Java rootpage-Generics_in_Java skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Generics in Java</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p><b>Generics</b> are a facility of <a href="Generic_programming" title="Generic programming">generic programming</a> that were added to the <a href="Java_(programming_language)" title="Java (programming language)">Java programming language</a> in 2004 within version <a href="Java_Platform%2C_Standard_Edition" title="Java Platform, Standard Edition">J2SE</a> 5.0. They were designed to extend Java's <a href="Type_system" title="Type system">type system</a> to allow "a type or method to operate on objects of various types while providing compile-time type safety".<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> The aspect <i><a href="Compile-time_type_safety" class="mw-redirect" title="Compile-time type safety">compile-time type safety</a></i> required that parametrically polymorphic
functions are not implemented in the <a href="Java_virtual_machine" title="Java virtual machine">Java virtual machine</a>, since type safety is impossible in this case.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-FOOTNOTEBloch2018123–125Chapter_§5_Item_27:_Eliminate_unchecked_warnings_3-0" class="reference"><a href="#cite_note-FOOTNOTEBloch2018123–125Chapter_§5_Item_27:_Eliminate_unchecked_warnings-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup>
</p><p>The <a href="Java_collections_framework" title="Java collections framework">Java collections framework</a> supports generics to specify the type of objects stored in a collection instance.
</p><p>In 1998, <a href="Gilad_Bracha" title="Gilad Bracha">Gilad Bracha</a>, <a href="Martin_Odersky" title="Martin Odersky">Martin Odersky</a>, David Stoutamire and <a href="Philip_Wadler" title="Philip Wadler">Philip Wadler</a> created Generic Java, an extension to the Java language to support generic types.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup> Generic Java was incorporated in Java with the addition of <a href="Wildcard_(Java)" title="Wildcard (Java)">wildcards</a>.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Hierarchy_and_classification">Hierarchy and classification</h2></div>
<p>According to <i>Java Language Specification</i>:<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup>
</p>
<ul><li>A <b><a href="Type_variable" title="Type variable">type variable</a></b> is an unqualified identifier. Type variables are introduced by generic class declarations, generic interface declarations, generic method declarations, and by generic constructor declarations.</li>
<li>A <b>class</b> is generic if it declares one or more type variables.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-0" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> It defines one or more type variables that act as parameters.<sup id="cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-0" class="reference"><a href="#cite_note-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> A generic class declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. All of these parameterized types share the same class at runtime.</li>
<li>An <b><a href="Interface_(Java)" title="Interface (Java)">interface</a></b> is generic if it declares one or more type variables.<sup id="cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-1" class="reference"><a href="#cite_note-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> It defines one or more type variables that act as parameters.<sup id="cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-2" class="reference"><a href="#cite_note-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> A generic interface declaration defines a set of types, one for each possible invocation of the type parameter section. All parameterized types share the same interface at runtime.</li>
<li>A <b>method</b> is generic if it declares one or more type variables.<sup id="cite_ref-FOOTNOTEBloch2018135–138Chapter_§5_Item_30:_Favor_generic_methods_8-0" class="reference"><a href="#cite_note-FOOTNOTEBloch2018135–138Chapter_§5_Item_30:_Favor_generic_methods-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup> These type variables are known as the formal type parameters of the method. The form of the formal type parameter list is identical to a type parameter list of a class or interface.</li>
<li>A <b>constructor</b> can be declared as generic, independently of whether the class that the constructor is declared in is itself generic. A constructor is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the constructor. The form of the formal type parameter list is identical to a type parameter list of a generic class or interface.</li></ul>
<div class="mw-heading mw-heading2"><h2 id="Motivation">Motivation</h2></div>
<p>The following block of Java code illustrates a problem that exists when not using generics. First, it declares an <b><code><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/ArrayList.html">ArrayList</a></code></b> of type <b><code><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/Object.html">Object</a></code></b>. Then, it adds a <code>String</code> to the <code>ArrayList</code>. Finally, it attempts to retrieve the added <code>String</code> and cast it to an <code>Integer</code>—an error in logic, as it impossible to cast any string instance to an integer.
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">List</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="p">();</span>
<span class="n">v</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="s">"test"</span><span class="p">);</span><span class="w"> </span><span class="c1">// A String that cannot be cast to an Integer</span>
<span class="kd">final</span><span class="w"> </span><span class="n">Integer</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Integer</span><span class="p">)</span><span class="w"> </span><span class="n">v</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"> </span><span class="c1">// Run time error</span>
</pre></div>
<p>Although the code is compiled without error, it throws a runtime exception (<code>java.lang.ClassCastException</code>) when executing the third line of code. This type of <a href="Logic_error" title="Logic error">logic error</a> can be detected during compile time by using generics<sup id="cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-3" class="reference"><a href="#cite_note-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> and is the primary motivation for using them.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-1" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> It defines one or more type variables that act as parameters.
</p><p>The above code fragment can be rewritten using generics as follows:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">String</span><span class="o">></span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><</span><span class="n">String</span><span class="o">></span><span class="p">();</span>
<span class="n">v</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="s">"test"</span><span class="p">);</span>
<span class="kd">final</span><span class="w"> </span><span class="n">Integer</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Integer</span><span class="p">)</span><span class="w"> </span><span class="n">v</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"> </span><span class="c1">// (type error) compilation-time error</span>
</pre></div>
<p>The type parameter <code>String</code> within the angle brackets declares the <code>ArrayList</code> to be constituted of <code>String</code> (a descendant of the <code>ArrayList</code>'s generic <code>Object</code> constituents). With generics, it is no longer necessary to cast the third line to any particular type, because the result of <code>v.get(0)</code> is defined as <code>String</code> by the code generated by the compiler.
</p><p>The logical flaw in the third line of this fragment will be detected as a <a href="Compile_time" title="Compile time">compile-time</a> error (with J2SE 5.0 or later) because the compiler will detect that <code>v.get(0)</code> returns <code>String</code> instead of <code>Integer</code>.<sup id="cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-4" class="reference"><a href="#cite_note-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> For a more elaborate example, see reference.<sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup>
</p><p>Here is a small excerpt from the definition of the interfaces <b><code><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/List.html">java.util.List</a></code></b> and <b><code><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/Iterator.html">java.util.Iterator</a></code></b> in package <b><code><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/package-summary.html">java.util</a></code></b>:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">interface</span> <span class="nc">List</span><span class="o"><</span><span class="n">E</span><span class="o">></span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">add</span><span class="p">(</span><span class="n">E</span><span class="w"> </span><span class="n">x</span><span class="p">);</span>
<span class="w"> </span><span class="n">Iterator</span><span class="o"><</span><span class="n">E</span><span class="o">></span><span class="w"> </span><span class="nf">iterator</span><span class="p">();</span>
<span class="p">}</span>
<span class="kd">interface</span> <span class="nc">Iterator</span><span class="o"><</span><span class="n">E</span><span class="o">></span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">E</span><span class="w"> </span><span class="nf">next</span><span class="p">();</span>
<span class="w"> </span><span class="kt">boolean</span><span class="w"> </span><span class="nf">hasNext</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Generic_class_definitions">Generic class definitions</h2></div>
<p>Here is an example of a generic Java class, which can be used to represent individual entries (key to value mappings) in a <a href="Map_(computer_science)" class="mw-redirect" title="Map (computer science)">map</a>:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">Entry</span><span class="o"><</span><span class="n">KeyType</span><span class="p">,</span><span class="w"> </span><span class="n">ValueType</span><span class="o">></span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span>
<span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="n">KeyType</span><span class="w"> </span><span class="n">key</span><span class="p">;</span>
<span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="n">ValueType</span><span class="w"> </span><span class="n">value</span><span class="p">;</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Entry</span><span class="p">(</span><span class="n">KeyType</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">ValueType</span><span class="w"> </span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">;</span>
<span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">KeyType</span><span class="w"> </span><span class="nf">getKey</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">key</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">ValueType</span><span class="w"> </span><span class="nf">getValue</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">value</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">toString</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">"("</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">key</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">", "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">")"</span><span class="p">;</span><span class="w"> </span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>This generic class could be used in the following ways, for example:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">String</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="o">></span><span class="w"> </span><span class="n">grade</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">String</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="o">></span><span class="p">(</span><span class="s">"Mike"</span><span class="p">,</span><span class="w"> </span><span class="s">"A"</span><span class="p">);</span>
<span class="kd">final</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">String</span><span class="p">,</span><span class="w"> </span><span class="n">Integer</span><span class="o">></span><span class="w"> </span><span class="n">mark</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">String</span><span class="p">,</span><span class="w"> </span><span class="n">Integer</span><span class="o">></span><span class="p">(</span><span class="s">"Mike"</span><span class="p">,</span><span class="w"> </span><span class="mi">100</span><span class="p">);</span>
<span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"grade: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">grade</span><span class="p">);</span>
<span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"mark: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">mark</span><span class="p">);</span>
<span class="kd">final</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">Integer</span><span class="p">,</span><span class="w"> </span><span class="n">Boolean</span><span class="o">></span><span class="w"> </span><span class="n">prime</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">Integer</span><span class="p">,</span><span class="w"> </span><span class="n">Boolean</span><span class="o">></span><span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">prime</span><span class="p">.</span><span class="na">getValue</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">prime</span><span class="p">.</span><span class="na">getKey</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">" is prime."</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">prime</span><span class="p">.</span><span class="na">getKey</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">" is not prime."</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>It outputs:
</p>
<pre>grade: (Mike, A)
mark: (Mike, 100)
13 is prime.
</pre>
<div class="mw-heading mw-heading2"><h2 id="Generic_method_definitions">Generic method definitions</h2></div>
<p>Here is an example of a generic method using the generic class above:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="o"><</span><span class="n">Type</span><span class="o">></span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">Type</span><span class="p">,</span><span class="w"> </span><span class="n">Type</span><span class="o">></span><span class="w"> </span><span class="nf">twice</span><span class="p">(</span><span class="n">Type</span><span class="w"> </span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">Type</span><span class="p">,</span><span class="w"> </span><span class="n">Type</span><span class="o">></span><span class="p">(</span><span class="n">value</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>Note: If we remove the first <code><Type></code> in the above method, we will get <a href="Compilation_error" title="Compilation error">compilation error</a> (cannot find symbol "Type"), since it represents the declaration of the symbol.
</p><p>In many cases, the user of the method need not indicate the type parameters, as they can be inferred:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">String</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="o">></span><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Entry</span><span class="p">.</span><span class="na">twice</span><span class="p">(</span><span class="s">"Hello"</span><span class="p">);</span>
</pre></div>
<p>The parameters can be explicitly added if needed:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">String</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="o">></span><span class="w"> </span><span class="n">pair</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Entry</span><span class="p">.</span><span class="o"><</span><span class="n">String</span><span class="o">></span><span class="n">twice</span><span class="p">(</span><span class="s">"Hello"</span><span class="p">);</span>
</pre></div>
<p>The use of <a href="Primitive_type" class="mw-redirect" title="Primitive type">primitive types</a> is not allowed, and <a href="Object_type_(object-oriented_programming)" class="mw-redirect" title="Object type (object-oriented programming)">boxed</a> versions must be used instead:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">pair</span><span class="p">;</span><span class="w"> </span><span class="c1">// Fails compilation. Use Integer instead.</span>
</pre></div>
<p>There is also the possibility to create generic methods based on given parameters.
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">public</span><span class="w"> </span><span class="o"><</span><span class="n">Type</span><span class="o">></span><span class="w"> </span><span class="n">Type</span><span class="o">[]</span><span class="w"> </span><span class="nf">toArray</span><span class="p">(</span><span class="n">Type</span><span class="p">...</span><span class="w"> </span><span class="n">elements</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">elements</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>In such cases you can't use primitive types either, e.g.:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="n">Integer</span><span class="o">[]</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">toArray</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">);</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Diamond_operator">Diamond operator</h2></div>
<p>Thanks to <a href="Type_inference" title="Type inference">type inference</a>, Java SE 7 and above allow the programmer to substitute an empty pair of angle brackets (<code><></code>, called the <i>diamond operator</i>) for a pair of angle brackets containing the one or more type parameters that a sufficiently close context <a href="Type_conversion#Implicit_type_conversion" title="Type conversion">implies</a>.<sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> Thus, the above code example using <code>Entry</code> can be rewritten as:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">String</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="o">></span><span class="w"> </span><span class="n">grade</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Entry</span><span class="o"><></span><span class="p">(</span><span class="s">"Mike"</span><span class="p">,</span><span class="w"> </span><span class="s">"A"</span><span class="p">);</span>
<span class="kd">final</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">String</span><span class="p">,</span><span class="w"> </span><span class="n">Integer</span><span class="o">></span><span class="w"> </span><span class="n">mark</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Entry</span><span class="o"><></span><span class="p">(</span><span class="s">"Mike"</span><span class="p">,</span><span class="w"> </span><span class="mi">100</span><span class="p">);</span>
<span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"grade: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">grade</span><span class="p">);</span>
<span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"mark: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">mark</span><span class="p">);</span>
<span class="kd">final</span><span class="w"> </span><span class="n">Entry</span><span class="o"><</span><span class="n">Integer</span><span class="p">,</span><span class="w"> </span><span class="n">Boolean</span><span class="o">></span><span class="w"> </span><span class="n">prime</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Entry</span><span class="o"><></span><span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">prime</span><span class="p">.</span><span class="na">getValue</span><span class="p">())</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">prime</span><span class="p">.</span><span class="na">getKey</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">" is prime."</span><span class="p">);</span>
<span class="k">else</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">prime</span><span class="p">.</span><span class="na">getKey</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">" is not prime."</span><span class="p">);</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Type_wildcards">Type wildcards</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Wildcard_(Java)" title="Wildcard (Java)">Wildcard (Java)</a></div>
<p>A type argument for a parameterized type is not limited to a concrete class or interface. Java allows the use of "type wildcards" to serve as type arguments for parameterized types. Wildcards are type arguments in the form "<code><?></code>"; optionally with an upper or lower <a href="Bounded_quantification" title="Bounded quantification">bound</a>. Given that the exact type represented by a wildcard is unknown, restrictions are placed on the type of methods that may be called on an object that uses parameterized types.
</p><p>Here is an example where the element type of a <code>Collection<E></code> is parameterized by a wildcard:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">Collection</span><span class="o"><?></span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><</span><span class="n">String</span><span class="o">></span><span class="p">();</span>
<span class="n">c</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="k">new</span><span class="w"> </span><span class="n">Object</span><span class="p">());</span><span class="w"> </span><span class="c1">// compile-time error</span>
<span class="n">c</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span><span class="w"> </span><span class="c1">// allowed</span>
</pre></div>
<p>Since we don't know what the element type of <code>c</code> stands for, we cannot add objects to it. The <code>add()</code> method takes arguments of type <code>E</code>, the element type of the <code>Collection<E></code> generic interface. When the actual type argument is <code>?</code>, it stands for some unknown type. Any method argument value we pass to the <code>add()</code> method would have to be a subtype of this unknown type. Since we don't know what type that is, we cannot pass anything in. The sole exception is <a href="Null_pointer" title="Null pointer">null</a>; which is a member of every type.<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup>
</p><p>To specify the <a href="Bounded_quantification" title="Bounded quantification">upper bound</a> of a type wildcard, the <code class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><span class="kd">extends</span></code> keyword is used to indicate that the type argument is a subtype of the bounding class.<sup id="cite_ref-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility_12-0" class="reference"><a href="#cite_note-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup> So <code class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><span class="n">List</span><span class="o"><?</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Number</span><span class="o">></span></code> means that the given list contains objects of some unknown type which extends the <code>Number</code> class. For example, the list could be <code>List<Float></code> or <code>List<Number></code>. Reading an element from the list will return a <code>Number</code>. Adding null elements is, again, also allowed.<sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup>
</p><p>The use of wildcards above adds flexibility<sup id="cite_ref-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility_12-1" class="reference"><a href="#cite_note-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup> since there is not any inheritance relationship between any two parameterized types with concrete type as type argument. Neither <code>List<Number></code> nor <code>List<Integer></code> is a subtype of the other; even though <code>Integer</code> is a subtype of <code>Number</code>.<sup id="cite_ref-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility_12-2" class="reference"><a href="#cite_note-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup> So, any method that takes <code>List<Number></code> as a parameter does not accept an argument of <code>List<Integer></code>. If it did, it would be possible to insert a <code>Number</code> that is not an <code>Integer</code> into it; which violates type safety. Here is an example that demonstrates how type safety would be violated if <code>List<Integer></code> were a subtype of <code>List<Number></code>:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Integer</span><span class="o">></span><span class="w"> </span><span class="n">ints</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><></span><span class="p">();</span>
<span class="n">ints</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
<span class="kd">final</span><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Number</span><span class="o">></span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ints</span><span class="p">;</span><span class="w"> </span><span class="c1">// valid if List<Integer> were a subtype of List<Number> according to substitution rule. </span>
<span class="n">nums</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="mf">3.14</span><span class="p">);</span><span class="w"> </span>
<span class="kd">final</span><span class="w"> </span><span class="n">Integer</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ints</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="w"> </span><span class="c1">// now 3.14 is assigned to an Integer variable!</span>
</pre></div>
<p>The solution with wildcards works because it disallows operations that would violate type safety:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">List</span><span class="o"><?</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Number</span><span class="o">></span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ints</span><span class="p">;</span><span class="w"> </span><span class="c1">// OK</span>
<span class="n">nums</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="mf">3.14</span><span class="p">);</span><span class="w"> </span><span class="c1">// compile-time error</span>
<span class="n">nums</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span><span class="w"> </span><span class="c1">// allowed</span>
</pre></div>
<p>To specify the lower bounding class of a type wildcard, the <code>super</code> keyword is used. This keyword indicates that the type argument is a supertype of the bounding class. So, <code class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><span class="n">List</span><span class="o"><?</span><span class="w"> </span><span class="kd">super</span><span class="w"> </span><span class="n">Number</span><span class="o">></span></code> could represent <code>List<Number></code> or <code>List<Object></code>. Reading from a list defined as <code class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><span class="n">List</span><span class="o"><?</span><span class="w"> </span><span class="kd">super</span><span class="w"> </span><span class="n">Number</span><span class="o">></span></code> returns elements of type <code>Object</code>. Adding to such a list requires either elements of type <code>Number</code>, any subtype of <code>Number</code> or <a href="Null_pointer" title="Null pointer">null</a> (which is a member of every type).
</p><p>The mnemonic PECS (Producer Extends, Consumer Super) from the book <b>Effective Java</b> by <a href="Joshua_Bloch" title="Joshua Bloch">Joshua Bloch</a> gives an easy way to remember when to use wildcards (corresponding to <a href="Covariance_and_contravariance_(computer_science)" title="Covariance and contravariance (computer science)">covariance</a> and <a href="Covariance_and_contravariance_(computer_science)" title="Covariance and contravariance (computer science)">contravariance</a>) in Java.<sup id="cite_ref-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility_12-3" class="reference"><a href="#cite_note-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Generics_in_throws_clause">Generics in throws clause</h2></div>
<p>Although exceptions themselves cannot be generic, generic parameters can appear in a throws clause:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">public</span><span class="w"> </span><span class="o"><</span><span class="n">T</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Throwable</span><span class="o">></span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">throwMeConditional</span><span class="p">(</span><span class="kt">boolean</span><span class="w"> </span><span class="n">conditional</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="n">exception</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">conditional</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="n">exception</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Problems_with_type_erasure">Problems with type erasure</h2></div>
<p>Generics are checked at compile-time for type-correctness.<sup id="cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-5" class="reference"><a href="#cite_note-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> The generic type information is then removed in a process called <a href="Type_erasure" title="Type erasure">type erasure</a>.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-2" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> For example, <code>List<Integer></code> will be converted to the non-generic type <code>List</code>, which ordinarily contains arbitrary objects. The compile-time check guarantees that the resulting code uses the correct type.<sup id="cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-6" class="reference"><a href="#cite_note-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup>
</p><p>Because of type erasure, type parameters cannot be determined at run-time.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-3" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> For example, when an <code>ArrayList</code> is examined at runtime, there is no general way to determine whether, before type erasure, it was an <code>ArrayList<Integer></code> or an <code>ArrayList<Float></code>. Many people are dissatisfied with this restriction.<sup id="cite_ref-14" class="reference"><a href="#cite_note-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup> There are partial approaches. For example, individual elements may be examined to determine the type they belong to; for example, if an <code>ArrayList</code> contains an <code>Integer</code>, that ArrayList may have been parameterized with <code>Integer</code> (however, it may have been parameterized with any parent of <code>Integer</code>, such as <code>Number</code> or <code>Object</code>).
</p><p>Demonstrating this point, the following code outputs "Equal":
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">final</span><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Integer</span><span class="o">></span><span class="w"> </span><span class="n">li</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><></span><span class="p">();</span>
<span class="kd">final</span><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">Float</span><span class="o">></span><span class="w"> </span><span class="n">lf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><></span><span class="p">();</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">li</span><span class="p">.</span><span class="na">getClass</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">lf</span><span class="p">.</span><span class="na">getClass</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// evaluates to true</span>
<span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"Equal"</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>Another effect of type erasure is that a generic class cannot extend the <code>Throwable</code> class in any way, directly or indirectly:<sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">GenericException</span><span class="o"><</span><span class="n">T</span><span class="o">></span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Exception</span>
</pre></div>
<p>The reason why this is not supported is due to type erasure:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">GenericException</span><span class="o"><</span><span class="n">Integer</span><span class="o">></span><span class="p">();</span>
<span class="p">}</span>
<span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">GenericException</span><span class="o"><</span><span class="n">Integer</span><span class="o">></span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">err</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"Integer"</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">GenericException</span><span class="o"><</span><span class="n">String</span><span class="o">></span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">err</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"String"</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>Due to type erasure, the runtime will not know which catch block to execute, so this is prohibited by the compiler.
</p><p>Java generics differ from <a href="C%2B%2B_Templates" class="mw-redirect" title="C++ Templates">C++ templates</a>. Java generics generate only one compiled version of a generic class or function regardless of the number of parameterizing types used. Furthermore, the Java run-time environment does not need to know which parameterized type is used because the type information is validated at compile-time and is not included in the compiled code. Consequently, instantiating a Java class of a parameterized type is impossible because instantiation requires a call to a constructor, which is unavailable if the type is unknown.
</p><p>For example, the following code cannot be compiled:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="o"><</span><span class="n">T</span><span class="o">></span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="nf">instantiateElementType</span><span class="p">(</span><span class="n">List</span><span class="o"><</span><span class="n">T</span><span class="o">></span><span class="w"> </span><span class="n">arg</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">T</span><span class="p">();</span><span class="w"> </span><span class="c1">//causes a compile error</span>
<span class="p">}</span>
</pre></div>
<p>Because there is only one copy per generic class at runtime, <a href="Static_variable" title="Static variable">static variables</a> are shared among all the instances of the class, regardless of their type parameter. Consequently, the type parameter cannot be used in the declaration of static variables or in static methods.
</p><p>Type erasure was implemented in Java to maintain <a href="Backward_compatibility" title="Backward compatibility">backward compatibility</a> with programs written prior to Java SE5.<sup id="cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-7" class="reference"><a href="#cite_note-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Differences_from_Arrays">Differences from Arrays</h2></div>
<p>There are several important differences between arrays (both primitive arrays and <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">Object</code> arrays), and generics in Java. Two of the major differences, namely, differences in terms of <a href="Variance_(computer_science)" class="mw-redirect" title="Variance (computer science)">variance</a> and <a href="Reification_(computer_science)" title="Reification (computer science)">reification</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Covariance,_contravariance_and_invariance">Covariance, contravariance and invariance</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Covariance_and_contravariance_(computer_science)" title="Covariance and contravariance (computer science)">Covariance and contravariance (computer science)</a></div>
<p>Generics are invariant, whereas arrays are <a href="Covariance_and_contravariance_(computer_science)" title="Covariance and contravariance (computer science)">covariant</a>.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-4" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> This is a benefit of using generic when compared to non-generic objects such as arrays.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-5" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> Specifically, generics can help prevent run time exceptions by throwing a compile-time exception to force the developer to fix the code.
</p><p>For example, if a developer declares an <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">Object[]</code> object and instantiates the object as a new <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">Long[]</code> object, no compile-time exception is thrown (since arrays are covariant).<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-6" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> This may give the false impression that the code is correctly written. However, if the developer attempts to add a <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">String</code> to this <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">Long[]</code> object, the program will throw an <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">ArrayStoreException</code>.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-7" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> This run-time exception can be completely avoided if the developer uses generics.
</p><p>If the developer declares a <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">Collection<Object></code> object an creates a new instance of this object with return type <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">ArrayList<Long></code>, the Java compiler will (correctly) throw a compile-time exception to indicate the presence of incompatible types (since generics are invariant).<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-8" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> Hence, this avoids potential run-time exceptions. This problem can be fixed by creating an instance of <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">Collection<Object></code> using <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">ArrayList<Object></code> object instead. For code using Java SE7 or later versions, the <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">Collection<Object></code> can be instantiated with an <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">ArrayList<></code> object using the <a class="mw-selflink-fragment" href="#Diamond_operator">diamond operator</a>
</p>
<div class="mw-heading mw-heading3"><h3 id="Reification">Reification</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Reification_(computer_science)" title="Reification (computer science)">Reification (computer science)</a></div>
<p>Arrays are <a href="Reification_(computer_science)" title="Reification (computer science)">reified</a>, meaning that an array object enforces its type information at run-time, whereas generics in Java are not reified.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-9" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>
</p><p>More formally speaking, objects with generic type in Java are non-reifiable types.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-10" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> A non-reifiable type is type whose representation at run-time has less information than its representation at compile-time.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-11" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>
</p><p>Objects with generic type in Java are non-reifiable due to type erasure.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-12" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> Java only enforces type information at compile-time. After the type information is verified at compile-time, the type information is discarded, and at run-time, the type information will not be available.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-13" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>
</p><p>Examples of non-reifiable types include <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">List<T></code> and <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">List<String></code>, where <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">T</code> is a generic formal parameter. <sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-14" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Project_on_generics">Project on generics</h2></div>
<p><a href="Project_Valhalla_(Java_language)" title="Project Valhalla (Java language)">Project Valhalla</a> is an experimental project to incubate improved Java generics and language features, for future versions potentially from Java 10 onwards. Potential enhancements include:<sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup>
</p>
<ul><li><a href="Generic_programming#Template_specialization" title="Generic programming">generic specialization</a>, e.g. List<int></li>
<li><a href="Comparison_of_C_Sharp_and_Java#Type_erasure_versus_reified_generics" title="Comparison of C Sharp and Java">reified generics</a>; making actual types available at runtime.</li></ul>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Generic_programming" title="Generic programming">Generic programming</a></li>
<li><a href="Template_metaprogramming" title="Template metaprogramming">Template metaprogramming</a></li>
<li><a href="Wildcard_(Java)" title="Wildcard (Java)">Wildcard (Java)</a></li>
<li><a href="Comparison_of_C_Sharp_and_Java" title="Comparison of C Sharp and Java">Comparison of C# and Java</a></li>
<li><a href="Comparison_of_Java_and_C%2B%2B" title="Comparison of Java and C++">Comparison of Java and C++</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="Citations">Citations</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://java.sun.com/j2se/1.5.0/docs/guide/language/index.html">Java Programming Language</a></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text">A ClassCastException can be thrown even in the absence of casts or nulls.<style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://raw.githubusercontent.com/namin/unsound/master/doc/unsound-oopsla16.pdf">"Java and Scala's Type Systems are Unsound"</a> <span class="cs1-format">(PDF)</span>.</cite></span>
</li>
<li id="cite_note-FOOTNOTEBloch2018123–125Chapter_§5_Item_27:_Eliminate_unchecked_warnings-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEBloch2018123–125Chapter_§5_Item_27:_Eliminate_unchecked_warnings_3-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFBloch2018">Bloch 2018</a>, pp. 123–125, Chapter §5 Item 27: Eliminate unchecked warnings.</span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://homepages.inf.ed.ac.uk/wadler/gj/">GJ: Generic Java</a></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html">Java Language Specification, Third Edition</a>
by James Gosling, Bill Joy, Guy Steele, Gilad Bracha – Prentice Hall PTR 2005</span>
</li>
<li id="cite_note-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays-6"><span class="mw-cite-backlink">^ <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-2"><sup><i><b>c</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-3"><sup><i><b>d</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-4"><sup><i><b>e</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-5"><sup><i><b>f</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-6"><sup><i><b>g</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-7"><sup><i><b>h</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-8"><sup><i><b>i</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-9"><sup><i><b>j</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-10"><sup><i><b>k</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-11"><sup><i><b>l</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-12"><sup><i><b>m</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-13"><sup><i><b>n</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§5_Item_28:_Prefer_lists_to_arrays_6-14"><sup><i><b>o</b></i></sup></a></span> <span class="reference-text"><a href="#CITEREFBloch2018">Bloch 2018</a>, pp. 126–129, Chapter §5 Item 28: Prefer lists to arrays.</span>
</li>
<li id="cite_note-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types-7"><span class="mw-cite-backlink">^ <a href="#cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-2"><sup><i><b>c</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-3"><sup><i><b>d</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-4"><sup><i><b>e</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-5"><sup><i><b>f</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-6"><sup><i><b>g</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018117–122Chapter_§5_Item_26:_Don't_use_raw_types_7-7"><sup><i><b>h</b></i></sup></a></span> <span class="reference-text"><a href="#CITEREFBloch2018">Bloch 2018</a>, pp. 117–122, Chapter §5 Item 26: Don't use raw types.</span>
</li>
<li id="cite_note-FOOTNOTEBloch2018135–138Chapter_§5_Item_30:_Favor_generic_methods-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEBloch2018135–138Chapter_§5_Item_30:_Favor_generic_methods_8-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFBloch2018">Bloch 2018</a>, pp. 135–138, Chapter §5 Item 30: Favor generic methods.</span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><cite id="CITEREFGilad_Bracha2004" class="citation web cs1">Gilad Bracha (July 5, 2004). <a rel="nofollow" class="external text" href="http://www.oracle.com/technetwork/java/javase/generics-tutorial-159168.pdf">"Generics in the Java Programming Language"</a> <span class="cs1-format">(PDF)</span>. <i>www.oracle.com</i>.</cite></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/7/docs/technotes/guides/language/type-inference-generic-instance-creation.html">"Type Inference for Generic Instance Creation"</a>.</cite></span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><cite id="CITEREFGilad_Bracha2004" class="citation web cs1">Gilad Bracha (July 5, 2004). <a rel="nofollow" class="external text" href="http://www.oracle.com/technetwork/java/javase/generics-tutorial-159168.pdf">"Generics in the Java Programming Language"</a> <span class="cs1-format">(PDF)</span>. <i>www.oracle.com</i>. p. 5.</cite></span>
</li>
<li id="cite_note-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility-12"><span class="mw-cite-backlink">^ <a href="#cite_ref-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility_12-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility_12-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility_12-2"><sup><i><b>c</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018139–145Chapter_§5_Item_31:_Use_bounded_wildcards_to_increase_API_flexibility_12-3"><sup><i><b>d</b></i></sup></a></span> <span class="reference-text"><a href="#CITEREFBloch2018">Bloch 2018</a>, pp. 139–145, Chapter §5 Item 31: Use bounded wildcards to increase API flexibility.</span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><cite id="CITEREFBracha" class="citation web cs1"><a href="Gilad_Bracha" title="Gilad Bracha">Bracha, Gilad</a>. <a rel="nofollow" class="external text" href="https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html">"Wildcards > Bonus > Generics"</a>. <i>The Java™ Tutorials</i>. Oracle. <q>...The sole exception is null, which is a member of every type...</q></cite></span>
</li>
<li id="cite_note-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-14">^</a></b></span> <span class="reference-text"><cite id="CITEREFGafter2006" class="citation web cs1">Gafter, Neal (2006-11-05). <a rel="nofollow" class="external text" href="http://gafter.blogspot.ro/2006/11/reified-generics-for-java.html">"Reified Generics for Java"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2010-04-20</span></span>.</cite></span>
</li>
<li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.2">"Java Language Specification, Section 8.1.2"</a>. Oracle<span class="reference-accessdate">. Retrieved <span class="nowrap">24 October</span> 2015</span>.</cite></span>
</li>
<li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text"><cite id="CITEREFGoetz" class="citation web cs1">Goetz, Brian. <a rel="nofollow" class="external text" href="http://mail.openjdk.java.net/pipermail/valhalla-dev/2014-July/000000.html">"Welcome to Valhalla!"</a>. <i>OpenJDK mail archive</i>. OpenJDK<span class="reference-accessdate">. Retrieved <span class="nowrap">12 August</span> 2014</span>.</cite></span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<ul><li><cite id="CITEREFBloch2018" class="citation book cs1">Bloch, Joshua (2018). <i>"Effective Java: Programming Language Guide"</i> (third ed.). Addison-Wesley. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0134685991</bdi>.</cite></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-05-24" href="https://en.wikipedia.org/wiki/?title=Generics_in_Java&oldid=1291963377">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>